package com.dk.academic.common;

import com.dk.academic.model.Article;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Utility {

    /**
     * 读取enw文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static Article enwAnalysis(File file){

        StringBuilder result = new StringBuilder();
        // 创建一个实体类用于存储相关解析数据
        Article article = new Article();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            String author = "";
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                // 判断行是否含有%0
                if (s.indexOf("%0") > -1) {
                    System.out.println(s);
                }
                // 判断作者
                if (s.indexOf("%A") > -1) author += s.replace("%A","");
                if (s.indexOf("%T") > -1) article.setTitle(s.replace("%T",""));
                if (s.indexOf("%J") > -1) article.setJournal(s.replace("%J",""));
                if (s.indexOf("%V") > -1) article.setVolume(Integer.valueOf(s.replace("%V ","")));
                if (s.indexOf("%N") > -1) article.setNumber(Integer.valueOf(s.replace("%N ","")));
                if (s.indexOf("%P") > -1) article.setPages(s.replace("%P",""));
                if (s.indexOf("%D") > -1) article.setYear(s.replace("%D",""));
                result.append(System.lineSeparator()+s);
            }
            article.setAuthor(author);
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return article;
    }
}
